package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.Strategy;
import cn.wolfcode.trip.base.domain.User;
import cn.wolfcode.trip.base.domain.UserWish;
import cn.wolfcode.trip.base.mapper.StrategyContentMapper;
import cn.wolfcode.trip.base.mapper.StrategyMapper;
import cn.wolfcode.trip.base.mapper.UserWishMapper;
import cn.wolfcode.trip.base.query.QueryObject;
import cn.wolfcode.trip.base.service.IStrategyService;
import cn.wolfcode.trip.base.util.UserContext;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class StrategyServiceImpl implements IStrategyService {

    @Autowired
    private StrategyMapper strategyMapper;
    @Autowired
    private StrategyContentMapper contentMapper;
    @Autowired
    private UserWishMapper wishMapper;

    @Override
    public int deleteByPrimaryKey(Long id) {
        return strategyMapper.deleteByPrimaryKey(id);
    }


    @Override
    public Strategy selectByPrimaryKey(Long id) {
        Strategy strategy = strategyMapper.selectByPrimaryKey(id);
        return strategy;
    }

    @Override
    public List<Strategy> selectAll() {
        return strategyMapper.selectAll();
    }

    @Override
    public void saveOrUpdate(Strategy strategy) {
        if (strategy.getId() == null) {
            strategyMapper.insert(strategy);
        }else{
            strategyMapper.updateByPrimaryKey(strategy);
        }

    }


    @Override
    public PageInfo getList(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize());
        List<Strategy> list = strategyMapper.getList(qo);
        return new PageInfo(list);
    }

    @Override
    public void saveWish(Long id,Integer state) {
        //当前登录用户
        User user = UserContext.getCurrentUser();
        UserWish uw = wishMapper.selectState(user.getId(),id);
        //要传入的对象
        UserWish wish = new UserWish();
        wish.setUserId(user.getId());
        wish.setStrategyId(id);
        if(uw == null){
            wish.setState(state);
            wishMapper.insert(wish);
        }
        if(uw != null && uw.getState() == 1){
            wish.setState(state);
            wishMapper.updateState(wish);
        }
    }

    @Override
    public List<Strategy> selectWish(int state) {
        List<Strategy> list = strategyMapper.selectWish(state,UserContext.getCurrentUser().getId());
        return list;
    }

    @Override
    public List<Strategy> selectByKeyword(QueryObject qo) {
        List<Strategy> list = strategyMapper.selectByKeyword(qo);
        return list;
    }


}
